package com.album.provider;

 
import com.album.bean.PicinfoList;
import com.album.bean.UserInfo;
import com.album.utils.MyLog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
 
public class ProviderUtil {
	public static final String TAG = "providerUtil";
	/**
	 * 绑定标识
	 */
	public static String ACITIVATE_FLAG = "activate_flag";
	/**
	 * 查询where参数占位
	 */
	public static String QUERY_WHERE_ARG = "=? ";
	/**
	 * 查询where参数占位
	 */
	public static String QUERY_WHERE_NOT_ARG = "!=? ";
	/**
	 * 查询and 关键字
	 */
	public static String QUERY_WHERE_AND = " and ";
	/**
	 * 查询or 关键字
	 */
	public static String QUERY_WHERE_OR = " or ";

	/**
	 * 左边括号
	 */
	public static String QUERY_WHERE_LEFT = " ( ";
	/**
	 * 右边括号
	 */
	public static String QUERY_WHERE_RIGHT = " ) ";

	/**
	 * 删除
	 * 
	 * @param cxt
	 */
	public synchronized static void delAllUserInfo(Context cxt) {
		cxt.getContentResolver().delete(AlbumPad.UserInfo.CONTENT_URI, null, null);
	}
	
	/**
	 * 获userBean信息
	 * 
	 * @return info(用户实体)
	 */
	public synchronized static UserInfo getUserInfo(Context cxt) {
		UserInfo info = new UserInfo();
		Cursor c = cxt.getContentResolver().query(AlbumPad.UserInfo.CONTENT_URI,
				null, null, null, null);
		if (null != c && c.getCount() != 0) {
			c.moveToFirst();
			info.itvId = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.itv_id));
			info.imsi = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.imsi));
			info.passport = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.passport));
			info.mobile = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.mobile));
			info.nick = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.nick));
			info.created = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.created));
			info.vcode = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.vcode));
			info.pause = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.pause));
			info.icon = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.icon));
			info.gender = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.gender));
			info.lastTime = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.last_time));
			info.status = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.status));
		}
		if (c != null) {
			c.close();
			c = null;
		}
		return info;
	}
	/**
	 * 获用户名字
	 * 
	 * @return info(用户实体)
	 */
	public synchronized static String getNick(Context cxt) {
		UserInfo info = new UserInfo();
		Cursor c = cxt.getContentResolver().query(AlbumPad.UserInfo.CONTENT_URI,
				null, null, null, null);
		if (null != c && c.getCount() != 0) {
			c.moveToFirst();
			info.nick = c.getString(c.getColumnIndexOrThrow(AlbumPad.UserInfo.nick));
		}
		if (c != null) {
			c.close();
			c = null;
		}
		return info.getNick();
	}
	/**
	 * UserInfo 增加
	 * 
	 * @param cxt
	 *            上下文
	 * @param UserInfo
	 *            业务信息
	 * @return 0 插入成功，－1 失败
	 */

	public synchronized static int insertOrUpdateUserInfo(Context cxt,UserInfo UserInfo) {
		// 更新serviceInfo 版本信息
		ContentValues values = new ContentValues(); 
		if(UserInfo.getItvId() ==null)UserInfo.setItvId("");
		if(UserInfo.getImsi() ==null)UserInfo.setImsi("");
		if(UserInfo.getPassport() ==null)UserInfo.setPassport("");
		if(UserInfo.getMobile() ==null)UserInfo.setMobile("");
		if(UserInfo.getNick() ==null)UserInfo.setNick("");
		if(UserInfo.getCreated() ==null)UserInfo.setCreated("");
		if(UserInfo.getVcode() ==null)UserInfo.setVcode("");
		if(UserInfo.getPause() ==null)UserInfo.setPause("");
		if(UserInfo.getIcon() ==null)UserInfo.setIcon("");
		if(UserInfo.getGender() ==null)UserInfo.setGender("");
		if(UserInfo.getLastTime() ==null)UserInfo.setLastTime("");
		if(UserInfo.getStatus() ==null)UserInfo.setStatus("");
		
		values.put(AlbumPad.UserInfo.itv_id, UserInfo.getItvId()); 
		values.put(AlbumPad.UserInfo.imsi, UserInfo.getImsi()); 
		values.put(AlbumPad.UserInfo.passport, UserInfo.getPassport());
		values.put(AlbumPad.UserInfo.mobile, UserInfo.getMobile());
		values.put(AlbumPad.UserInfo.nick, UserInfo.getNick());
		values.put(AlbumPad.UserInfo.created, UserInfo.getCreated());
		values.put(AlbumPad.UserInfo.vcode, UserInfo.getVcode());
		values.put(AlbumPad.UserInfo.pause, UserInfo.getPause());
		values.put(AlbumPad.UserInfo.icon, UserInfo.getIcon());
		values.put(AlbumPad.UserInfo.gender, UserInfo.getGender());
		values.put(AlbumPad.UserInfo.last_time, UserInfo.getLastTime());
		values.put(AlbumPad.UserInfo.status, UserInfo.getStatus()); 
		
		String selection = AlbumPad.UserInfo.mobile + QUERY_WHERE_ARG;
		String[] selectionArgs = { UserInfo.getMobile() };
		Cursor c = cxt.getContentResolver().query(AlbumPad.UserInfo.CONTENT_URI,
				null, selection, selectionArgs, null);
		if (null != c && c.getCount() != 0) {
			cxt.getContentResolver().update(AlbumPad.UserInfo.CONTENT_URI, values,
					selection, selectionArgs);
		} else {
			cxt.getContentResolver().insert(AlbumPad.UserInfo.CONTENT_URI, values);
		}

		if (c != null) {
			c.close();
			c = null;
		}
		return 0;
	}
	/**
	 * 删除
	 * 
	 * @param cxt
	 */
	public synchronized static void delAllPicinfoList(Context cxt) {
		cxt.getContentResolver().delete(AlbumPad.UserInfo.CONTENT_URI, null, null);
	}
	
	/**
	 * 获sellingBean信息
	 * 
	 * @return info(用户实体)
	 */
	public synchronized static PicinfoList getPicinfoList(Context cxt) {
		PicinfoList info = new PicinfoList();
		Cursor c = cxt.getContentResolver().query(AlbumPad.UserInfo.CONTENT_URI,
				null, null, null, null);
		if (null != c && c.getCount() != 0) {
			c.moveToFirst();
			info.fileid = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.fileid));
			info.passport = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.passport));
			info.public_ = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.public_));
			info.typeid = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.typeid));
			info.upload_time = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.upload_time));
			info.title = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.title));
			info.content = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.content));
			info.views = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.views));
			info.score = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.score));
			info.block = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.block));
			info.area = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.area));
			info.coordinate = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.coordinate));
			info.hash_code = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.hash_code));
			info.path_image = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.path_image));
			info.exif = c.getString(c.getColumnIndexOrThrow(AlbumPad.PicinfoList.exif));
		}
		if (c != null) {
			c.close();
			c = null;
		}
		return info;
	}
	/**
	 * UserInfo 增加
	 * 
	 * @param cxt
	 *            上下文
	 * @param UserInfo
	 *            业务信息
	 * @return 0 插入成功，－1 失败
	 */

	public synchronized static int insertOrUpdatePicinfoList(Context cxt,PicinfoList picinfo) {
		// 更新serviceInfo 版本信息
		ContentValues values = new ContentValues(); 
		values.put(AlbumPad.PicinfoList.fileid, picinfo.getFileid()); 
		values.put(AlbumPad.PicinfoList.passport, picinfo.getPassport()); 
		values.put(AlbumPad.PicinfoList.public_, picinfo.getPublic_());
		values.put(AlbumPad.PicinfoList.typeid, picinfo.getTypeid());
		values.put(AlbumPad.PicinfoList.upload_time, picinfo.getUpload_time());
		values.put(AlbumPad.PicinfoList.title, picinfo.getTitle());
		values.put(AlbumPad.PicinfoList.content, picinfo.getContent());
		values.put(AlbumPad.PicinfoList.views, picinfo.getViews());
		values.put(AlbumPad.PicinfoList.score, picinfo.getScore());
		values.put(AlbumPad.PicinfoList.block, picinfo.block);
		values.put(AlbumPad.PicinfoList.area, picinfo.getArea());
		values.put(AlbumPad.PicinfoList.coordinate, picinfo.getCoordinate());
		values.put(AlbumPad.PicinfoList.hash_code, picinfo.getHash_code()); 
		values.put(AlbumPad.PicinfoList.path_image, picinfo.getPath_image());
		values.put(AlbumPad.PicinfoList.exif, picinfo.getExif()); 
		
		String selection = AlbumPad.PicinfoList.fileid + QUERY_WHERE_ARG;
		String[] selectionArgs = { picinfo.getFileid() };
		Log.i("", "insertOrUpdatePicinfoList:"+selection);
		Log.i("", "insertOrUpdatePicinfoList:"+selectionArgs);
		Cursor c = cxt.getContentResolver().query(AlbumPad.PicinfoList.CONTENT_URI,
				null, selection, selectionArgs, null);
		if (null != c && c.getCount() != 0) {
			cxt.getContentResolver().update(AlbumPad.PicinfoList.CONTENT_URI, values,
					selection, selectionArgs);
		} else {
			cxt.getContentResolver().insert(AlbumPad.PicinfoList.CONTENT_URI, values);
		}

		if (c != null) {
			c.close();
			c = null;
		}
		return 0;
	}
	/**
	 * 判断数据是否为空
	 * @param cxt
	 * @param UserInfo
	 * @return
	 */
	public static boolean isSqlEmpty(Context cxt) {
		Cursor c = null;
		try {
			c = cxt.getContentResolver().query(
					AlbumPad.UserInfo.CONTENT_URI, null, null, null, null); 
			if (null != c && c.getCount() != 0) {
				c.close();
				c = null;
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return true;
	}
}
